import SiderBav from '@/components/sider-nav/index'
export default {
  name: 'crowdsList',
  components: {
    SiderBav
  },
  data() {
    return {
      campaign_id:'',
      campaign_Name:'',
      planNav: [
      {
        icon: "",
        text: '移除定向',
        router: '',
        fn(){
          this.$parent.delet();
        }
      }
    ],
      options: [{
          value: '',
          label: '全部状态'
        },
        {
          value: '0',
          label: '暂停'
        },
        {
          value: '1',
          label: '投放中'
        },
        {
          value: '9',
          label: '投放结束'
        }
      ],
      planStatus: '',
      dataList: [],
      total_count: 0,
      searchPlanName: "",
      searchOptions: {},
      checkList:[],
      checkListAll:false,
      loading:false,
      currentPage:1
    }
  },
  // computed: {
  //   planSidebar() {
  //     return this.$store.state.planSidebar
  //   }
  // },
  watch: {
    planStatus(val) {
      this.search();
    },
    checkListAll(val){
        this.checkList=[];
        if(val){
          this.dataList.forEach(element => {
                this.checkList.push(element.id+'/'+element.adgroup_id);
          });
        }
    }
  },
  mounted() {
    console.log(this.$route.query);
    this.campaign_id=this.$route.query.id;
    this.campaign_Name=this.$route.query.name;
    this.day_budget=this.$route.query.day_budget;
    this.search();
  },
  filters: {
    planStatus: function (n) {
      return {
        0: "暂停",
        1: '投放中',
        9: '投放结束'
      } [n];
    }
  },
  methods: {
    pagechange(val) {
      this.search(val);
    },
    search(page) {
      let page_num = page || 1;
      let params = {
        // page_num: page_num,
        // page_size: 20
      };
      params = Object.assign({}, params, this.searchOptionsfn().params);
      this.dataList = [];
			this.total_count = 0;
			if(typeof this.source=='function'){
             this.source();//取消请求
      }
      this.loading=true;
      const loading = this.$loading({
        lock: true,
        text: '数据获取中....',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
        customClass:'lodding'
      });
      let _this=this;
      this.currentPage=page_num;
      this.$http({
          method: 'get',
          url: this.searchOptions.url,
					params: params,
        }).then((data) => {
          loading.close();
          this.loading=false;
          //if (!this.$http.interceptors(this, data)) return;

          // for (let i = 0, o; o = data.data.data.list[i++];) {
          //   o['hover'] = false;
          // }
          console.log('data.data.data',data.data.data);
          this.dataList = data.data.data;
          // this.total_count = data.data.data.total_count;
        })
        .catch(function (error) {
          _this.loading=false;
          loading.close();
          console.log(error);
        });
    },
    trhoverFn(o) {
      for (let i = 0, o; o = this.dataList[i++];) {
        o['hover'] = false;
      }
      o.hover = true;
    },
    delet() {
      if(this.checkList.length<=0){
            this.$message.error('未选择定向');
            return false;
        }
        this.$confirm('确定移除定向?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let json={
            campaign_id:this.campaign_id,
            crowds:[]
          };
          this.checkList.forEach(element=>{
            json.crowds.push({
                adgroup_id:element.split('/')[1],
                crowd_id:element.split('/')[0]
              });
          })        
          this.$http({
            method: 'post',
            url: '/zuanzhan/crowd/del',
            data: json
          }).then((data) => {
            this.$message.success('移除成功');
            this.search();
          })
          .catch(function (error) {
            console.log(error);
          });
        }).catch(() => {         
        });
    },
    searchOptionsfn(){
      this.searchOptions = {};
      this.searchOptions.url = '/zuanzhan/crowd/all';
      this.searchOptions.params = {
        campaign_id: this.campaign_id
        // online_status: this.planStatus
      }
      return this.searchOptions;
    },
    changestatus(status){
        if(this.checkList.length<=0){
            this.$message.error('未选择计划');
            return false;
        }
        this.$confirm('确定修改计划状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           this.$http({
             method: 'post',
             url: '/zuanzhan/campaigns/status',
             data:{
               campaign_id_list:this.checkList,
               status:status+''
             },
           }).then((data) => {
               if (!this.$http.interceptors(this, data)) return;
               this.$message({
                 type: 'success',
                 message: '修改成功!'
               });
               this.search();
           })
           .catch(function (error) {
             console.log(error);
           });
        }).catch(() => {         
        });
    },
    toCrowds(obj){
        console.log('feafawef',obj);
    }
  }
}